class DocumentCorrectionsController < ApplicationController
  load_and_authorize_resource

  # GET /document_corrections
  # GET /document_corrections.xml
  def index
    @document_corrections = DocumentCorrection.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @document_corrections }
    end
  end

  # GET /document_corrections/1
  # GET /document_corrections/1.xml
  def show
    @document_correction = DocumentCorrection.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @document_correction }
    end
  end

  # GET /document_corrections/new
  # GET /document_corrections/new.xml
  def new
    if params[:document_id]
      @document_correction = DocumentCorrection.from_document(Document.find(params[:document_id]))
    elsif params[:section_id]
      @document_correction = DocumentCorrection.from_section(Document.find(params[:section_id]))
    end

    respond_to do |format|
      format.html { render :layout=> !request.xhr? }
      format.xml  { render :xml => @document_correction }
    end
  end

  # GET /document_corrections/1/edit
  def edit
    @document_correction = DocumentCorrection.find(params[:id])
  end

  # POST /document_corrections
  # POST /document_corrections.xml
  def create
    @document_correction = DocumentCorrection.new(params[:document_correction])

    respond_to do |format|
      if @document_correction.save
        flash[:notice] = t("document_correction.created_succesfully")
        format.html { redirect_to(document_path(@document_correction.document)) }
        format.xml  { render :xml => @document_correction, :status => :created, :location => @document_correction }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @document_correction.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /document_corrections/1
  # PUT /document_corrections/1.xml
  def update
    @document_correction = DocumentCorrection.find(params[:id])

    respond_to do |format|
      if @document_correction.update_attributes(params[:document_correction])
        flash[:notice] = 'DocumentCorrection was successfully updated.'
        format.html { redirect_to(@document_correction) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @document_correction.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /document_corrections/1
  # DELETE /document_corrections/1.xml
  def destroy
    @document_correction = DocumentCorrection.find(params[:id])
    @document_correction.destroy

    respond_to do |format|
      format.html { redirect_to(document_corrections_url) }
      format.xml  { head :ok }
    end
  end
end

